package com.yc.c110s2plyqnzf.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.c110s2plyqnzf.entity.House;
import com.yc.c110s2plyqnzf.mapper.HouseMapper;
import com.yc.c110s2plyqnzf.service.IHouseService;
import com.yc.c110s2plyqnzf.service.impl.HouseServiceImpl;
import com.yc.c110s2plyqnzf.vo.Result;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 青鸟租房-房屋表-2 前端控制器
 * </p>
 *
 * @author yc
 * @since 2022-09-03
 */
@RestController
@RequestMapping("/c110s2plyqnzf/house")
public class HouseController {

    @Resource
    private HouseMapper houseMapper;

    @Resource
    private IHouseService houseService;

    @RequestMapping("query")
    public Page<House> query( @RequestParam(defaultValue = "1") int page,
                              @RequestParam(defaultValue = "5") int size ) {
        Page<House> pg = new Page<>(page, size);
        pg = houseMapper.selectPage(pg, null);
        return pg;
    }

    @RequestMapping("save")
    public Result save(@Valid House house, Errors errors) {
        if (errors.hasErrors()) {
            return new Result(0, "字段验证错误!", errors.getFieldErrors());
        }
        houseService.saveOrUpdate(house);
        return new Result(1, "房屋添加成功", null);
    }

    @RequestMapping("queryById")
    public House queryById(int id) {
        return houseMapper.selectById(id);
    }

    @RequestMapping("deleteById")
    public Result deleteById(int id) {
        try{
            houseMapper.deleteById(id);
            return new Result(1, "删除成功", null);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(0, "删除失败,请联系管理员", null);
        }

    }

}

